<template>
  <div>
    <div class="login_container">
      <el-form :label-position="labelPosition" label-width="80px" :model="from">
        <el-form-item label="用户名">
          <el-input v-model="from.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="from.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      labelPosition: "top",
      from: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    // 登录
    login() {
      this.$axios({
        method: "post",
        url: "/login",
        data: this.from,
      }).then((res) => {
        console.log(res);
        if (res.data.message == "登录成功") {
          this.$message.success(res.data.message);
          localStorage.setItem("token", res.data.data.token);
          localStorage.setItem("userId", res.data.data.user.id);
          this.$router.replace("/");
        }
      });
    },
    // 重置
    reset() {
      this.from = {
        username: "",
        password: "",
      };
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url("../assets/login_bg.jpg") no-repeat center/cover;
}
</style>
